package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

//	http://127.0.0.1/api/account/userAccount/getAvailableAmount
	@TsLogin
	@Operation(summary = "查询用户账户余额")
	@GetMapping("/getAvailableAmount")
	public Result getAvailableAmount() {
//		从线程中获取用户id
		Long userId = AuthContextHolder.getUserId();
		return Result.ok(userAccountService.getAvailableAmount(userId));
	}

//	@PostMapping("api/account/userAccount/checkAndDeduct")
//    Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo);
	@Operation(summary = "用户账户扣款")
	@PostMapping("/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
//		调用service层的方法
		Boolean flag = userAccountService.checkAndDeduct(accountDeductVo);
		return Result.ok(flag);
	}


}

